package main

import (
	"fmt"
)

// map 操作

func integerReplacement(n int) int {
	result := 0
	if n == 1 {
		return 0
	} else if n%2 == 0 {
		result = 1 + integerReplacement(n/2)
		return result
	} else {
		var min int
		if integerReplacement(n/2) > integerReplacement(n/2+1) {
			min = integerReplacement(n/2 + 1)
		} else {
			min = integerReplacement(n / 2)
		}
		result = min + 2
		return result
	}
}

func sortArrayByParity(nums []int) []int {
	var oList, jList []int
	for i := 0; i < len(nums); i++ {
		if nums[i]%2 == 0 {
			oList = append(oList, nums[i])
		} else {
			jList = append(jList, nums[i])
		}
	}
	oList = append(oList, jList...)
	return oList
}

func sortArrayByParity01(nums []int) []int {
	idx := 0
	for i := 0; i < len(nums); i++ {
		if nums[i]%2 == 0 {
			nums[idx], nums[i] = nums[i], nums[idx]
			idx += 1
		}
	}

	return nums
}

func main() {
	var i int = 4
	fmt.Printf("%d 的阶乘是 %d\n", i, integerReplacement(int(i)))

	nums := []int{3, 1, 2, 4}
	fmt.Println(sortArrayByParity(nums))
	fmt.Println(sortArrayByParity01(nums))
}
